package com.atguigu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SpuInfoDescDao;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.service.SpuInfoDescService;
import org.springframework.util.ObjectUtils;


@Service("spuInfoDescService")
public class SpuInfoDescServiceImpl extends ServiceImpl<SpuInfoDescDao, SpuInfoDescEntity> implements SpuInfoDescService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoDescEntity> page = this.page(
                new Query<SpuInfoDescEntity>().getPage(params),
                new QueryWrapper<SpuInfoDescEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDecript(Long spuInfoId, List<String> decript) {
        if(!ObjectUtils.isEmpty(decript)){
            String decriptStr = decript.stream().collect(Collectors.joining(",", "", ""));
            SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity(spuInfoId,decriptStr);
            this.save(spuInfoDescEntity);
        }
    }

    @Override
    public SpuInfoDescEntity getBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuInfoDescEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuInfoDescEntity::getSpuId,spuId);
        SpuInfoDescEntity spuInfoDescEntity = baseMapper.selectOne(lambdaQueryWrapper);
        return spuInfoDescEntity;
    }

}